package com.molichuxing.job;

import com.molichuxing.constants.RedisConstants;
import com.molichuxing.framework.utils.RedisUtil;
import com.molichuxing.service.MileageMonitoringService;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.IJobHandler;
import com.xxl.job.core.handler.annotation.JobHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.LocalDate;


/**
 * The type Mileage monitoring handler.
 * 生成车辆里程监控
 *  常量
 * @author wanghualin
 * @date 2020 -06-10 15:50:02
 */
@JobHandler(value = "mileageMonitoringHandler")
@Component
@Slf4j
public class MileageMonitoringHandler extends IJobHandler {
    @Resource
    private RedisUtil redisUtil;
    @Autowired
    private MileageMonitoringService mileageMonitoringService;

    @Override
    public ReturnT<String> execute(String s) throws Exception {
        ReturnT<String> returnT = ReturnT.SUCCESS;
        log.info("==============生成车辆里程监控定时任务start================");
        LocalDate localDate = LocalDate.now();
        try {
            // 生成里程监控
            mileageMonitoringService.generateMileageMonitoring(localDate);
            log.info("生成报警完成");
        } catch (Exception e) {
            int day = localDate.getDayOfMonth();
            if (day<=28){
                redisUtil.hset(RedisConstants.MILEAGE_MONITORING_EXCEPTION_DATE_KEY, localDate.toString(), localDate.toString());
            }
            log.info("生成车辆里程监控定时任务异常");
            e.printStackTrace();
        }
        mileageMonitoringService.updateMileageMonitoring();

        mileageMonitoringService.generateAbnormalDataMileageMonitoring();

        log.info("==============生成车辆里程监控定时任务end================");
        return returnT;
    }
}
